<?php
	include("includes/config.php");
	include("includes/limba/ro/limba.php");
	if(@$_POST['intensitate']){
		$intensitate = mysql_real_escape_string($_POST['intensitate']);
		$ip_user = mysql_real_escape_string($_POST['user_ip']);
		$eveniment = mysql_real_escape_string($_POST['eveniment']);
		$adresa = mysql_real_escape_string($_POST['adresa']);
		$adresa = iconv('UTF-8', 'ASCII//TRANSLIT',$adresa);
		$latlng = mysql_real_escape_string($_POST['latlng']);
		
		if(preg_match("/err/",$adresa)){
			if( ($ip_user == "") || ($ip_user == "127.0.0.1")){
				$adresa = "nu a putut fi localizat";
				$latlng = "44.4333,26.1000";
			}else{
			$jsonIP = file_get_contents("http://ipinfo.io/".$ip_user."/json");
			$jsonIP = json_decode($jsonIP);
			$adresa = $jsonIP->city;
			$latlng = $jsonIP->loc;
			}
		}
		$device = mysql_real_escape_string($_POST['device']);
		$raspuns = $_['raspuns-'.$intensitate];
		
		if(@$_COOKIE['utilizator']){
			if($_COOKIE['utilizator'] == md5($eveniment)){
				echo "err";
			}else{
				$res = mysql_query("select * from evenimente where id_cutremur = '$eveniment'");
					if(mysql_num_rows($res)>=1){
setcookie("utilizator", md5($eveniment), time()+3600);
					mysql_query("insert into feedback 
					 (id_raspuns,id_eveniment,raspuns,intensitate,comentariu,timp,ip_raspuns,adresa,coordonate,device,data_raspuns) VALUES
						(null,'$eveniment','$raspuns','$intensitate','',now(),'$ip_user','$adresa','$latlng','$device',now())"
					);
					echo mysql_insert_id();
					
				}
			}
		}else{
			$res = mysql_query("select * from evenimente where id_cutremur = '$eveniment'");
				if(mysql_num_rows($res)>=1){
setcookie("utilizator", md5($eveniment), time()+3600);
				mysql_query("insert into feedback 
				 (id_raspuns,id_eveniment,raspuns,intensitate,comentariu,timp,ip_raspuns,adresa,coordonate,device,data_raspuns) VALUES
					(null,'$eveniment','$raspuns','$intensitate','',now(),'$ip_user','$adresa','$latlng','$device',now())"
				);
				echo mysql_insert_id();
			}
		}
	}
	if(@$_POST['comentariu']){
			$comentariu = mysql_real_escape_string($_POST['comentariu']);
			$id_insert = mysql_real_escape_string($_POST['id_insert']);
			mysql_query("update feedback set comentariu = '$comentariu' where id_raspuns = '$id_insert'");
	}
	if(@$_POST['comentarii']){
		$idEveniment = mysql_real_escape_string($_POST['idEveniment']);
		$res = mysql_query("select * from feedback where id_eveniment = '$idEveniment' and  comentariu !='' order by data_raspuns asc");
		$arr = array();
		while($row = mysql_fetch_array($res)){
			$arr[] = $row;
		}
		
		if(count($arr) > 0){
				echo json_encode($arr);
		}
		
	}
?>